home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / game / misc / InitPoolSrc.lha / InitPool.e < prev   
Text File  |  1999-01-02  |  12KB  |  462 lines

  1. /******************************************/
  2. /*                                        */
  3. /* Initiative Pool Manager  v1.00 (Amiga) */
  4. /*                                        */
  5. /*   Copyright (c) J.Gregory 21/04/1998   */
  6. /*                                        */
  7. /*    Uses Text plugin by Ali Graham      */
  8. /*                                        */
  9. /******************************************/
  10.  
  11. /* The text plugin has been modified to allow contents
  12.    to be changed after creation.  Thr initial contents
  13.    must however be the widest intended content. Hence
  14.    all of the 'MMM's in the source below.
  15. */
  16.  
  17. OPT PREPROCESS, OSVERSION=37
  18.  
  19. MODULE 'tools/easygui',
  20.        'tools/constructors',
  21.        'easyplugins/text2',
  22.        'plugins/ticker',
  23.        'exec/nodes',
  24.        'exec/lists',
  25.        'graphics/text',
  26.        'intuition/intuition',
  27.        'dos/dos',
  28.        'utility',
  29.        'utility/tagitem',
  30.        'diskfont'
  31.  
  32. /*****************/
  33. /*** Constants ***/
  34. /*****************/
  35.  
  36. CONST MAX_UNITS=400
  37. ENUM  UNIT_WAIT,UNIT_MOVE,UNIT_DEAD
  38.  
  39. /*****************************/
  40. /*** Structure Definitions ***/
  41. /*****************************/
  42.  
  43. OBJECT unitdata
  44.   status
  45.   id
  46.   flag
  47.   prefix[2]:ARRAY OF CHAR
  48. ENDOBJECT
  49.  
  50. OBJECT unititem                     /*** Exec List Version of units array  ***/
  51.   node:ln,                          /*            Exec list node             */
  52.   id,                               /*          Array index number           */
  53.   name[16]:ARRAY OF CHAR            /*         Text Details String           */
  54. ENDOBJECT
  55.  
  56. /*******************/
  57. /*** Global Data ***/
  58. /*******************/
  59.  
  60. DEF mgh=NIL:PTR TO guihandle
  61. DEF text_a:PTR TO text_plugin
  62. DEF totunits,current,lastn=-1,log=TRUE
  63. DEF unitlist=NIL:PTR TO lh
  64. DEF last[10]:STRING,dispstr[80]:STRING
  65. DEF units[MAX_UNITS]:ARRAY OF unitdata
  66.  
  67. /*********************/
  68. /*** Main Function ***/
  69. /*********************/
  70.  
  71. PROC main() HANDLE
  72.     DEF tim =NIL:PTR TO ticker
  73.     DEF ta  =NIL:PTR TO textattr
  74.     DEF font=NIL:PTR TO LONG
  75.     DEF ret
  76.  
  77.     IF (utilitybase :=OpenLibrary('utility.library', 37))=NIL THEN Raise("utlb")
  78.     IF (diskfontbase:=OpenLibrary('diskfont.library',37))=NIL THEN Raise("dlib")
  79.  
  80.     ta:=['CGTriumvirate.font', 50, 0, 0]:textattr
  81.     font:=OpenDiskFont(ta)
  82.     IF font=NIL THEN Raise("font")
  83.  
  84.     IF readdata() THEN Raise("load")
  85.  
  86.     NEW tim
  87.     NEW text_a.text([PLA_Text_Text, 'MMMMMMMMMMMMM',
  88.                      PLA_Text_Justification, PLV_Text_JustifyCenter,
  89.                      PLA_Text_Font, ta,
  90.                      TAG_DONE])
  91.  
  92.     mgh:=guiinitA('Initiative Pool Manager',
  93.              [ROWS,
  94.                  [PLUGIN, 1, text_a],
  95.                  [BAR],
  96.                  [COLS,
  97.                     [CHECK,   {logtog},    'Log',log,TRUE],
  98.                     [SBUTTON, {nexthit},   '  Next  '],
  99.                     [SBUTTON, {unitslist}, ' Status '],
  100.                     [SBUTTON, 0,           '  Quit  ']
  101.                     ],
  102.                  [PLUGIN, {tiktoc}, tim]
  103.                  ])
  104.  
  105.     text_a.set(PLA_Text_Text, 'Hit Next')
  106.  
  107.     ret:=1
  108.     WHILE ret<>0
  109.       Wait(mgh.sig)
  110.       ret:=guimessage(mgh)
  111.       IF ret=0                                                   /* Verify Exit */
  112.         ret:=butrequest('InitPool Request','Quit, Are you sure ?',' Quit |Cancel')
  113.         IF ret=0 THEN ret:=-1 ELSE ret:=0
  114.       ENDIF
  115.     ENDWHILE
  116.  
  117. EXCEPT DO
  118.     SELECT exception
  119.       CASE "load"
  120.         WriteF('Units data file load failure\n')
  121.       CASE "font"
  122.         WriteF('CGTriumvirate font open failed\n')
  123.       CASE "utlb"
  124.         WriteF('Utility.library open failure\n')
  125.       CASE "dlib"
  126.         WriteF('Diskfont.library open failure\n')
  127.     ENDSELECT
  128.  
  129.     IF mgh<>NIL THEN cleangui(mgh)
  130.  
  131.     END text_a
  132.     END tim
  133.  
  134.     IF font THEN CloseFont(font)
  135.  
  136.     IF diskfontbase THEN CloseLibrary(diskfontbase)
  137.     IF utilitybase  THEN CloseLibrary(utilitybase)
  138. ENDPROC
  139.  
  140.  
  141. /*********************************/
  142. /*** Display Warning Requester ***/
  143. /*********************************/
  144.  
  145. /* Require intuition/intuition.m */
  146.  
  147. PROC butrequest(title:PTR TO CHAR,text:PTR TO CHAR,gad:PTR TO CHAR) HANDLE
  148.   DEF ret,active=0
  149.  
  150.   blockwin(mgh)
  151.   active:=1
  152.  
  153.   ret:=EasyRequestArgs(mgh.wnd,[SIZEOF easystruct,0,title,text,gad]:easystruct,
  154.                        NIL,NIL)
  155. EXCEPT DO
  156.   IF active<>0 THEN unblockwin(mgh)
  157. ENDPROC ret
  158.  
  159. /*****************************/
  160. /*** Read Config Data File ***/
  161. /*****************************/
  162.  
  163. /* Returns 0 on success */
  164.  
  165. PROC readdata() HANDLE
  166.   DEF handle=NIL,str,len,err=0
  167.   DEF buffer[128]:ARRAY OF CHAR
  168.  
  169.   str:=1
  170.   current:=totunits:=0
  171.   last[0]:=0
  172.  
  173.   handle:=Open('InitPool.txt',MODE_OLDFILE)
  174.   IF handle=NIL THEN Raise("open")
  175.  
  176.   WHILE str<>NIL
  177.     str:=Fgets(handle,buffer,128)
  178.     IF str<>NIL
  179.       len:=StrLen(str)
  180.       IF (len>=10) AND (str[0]<>";")
  181.         len:=addunits(str)
  182.         IF len<>0
  183.           str:=NIL
  184.           err:=-1
  185.         ENDIF
  186.       ENDIF
  187.     ENDIF
  188.   ENDWHILE
  189.  
  190.   totunits:=current;
  191.   IF totunits<1 THEN err:=-1
  192. EXCEPT DO
  193.   IF handle<>NIL THEN Close(handle);
  194. ENDPROC err
  195.  
  196. /**********************************************/
  197. /*** Convert data file line to unit entries ***/
  198. /**********************************************/
  199.  
  200. /* Returns 0 on success */
  201.  
  202. PROC addunits(str)
  203.   DEF start,end,n,flag
  204.  
  205.   str[2]:=0
  206.   str[5]:=0
  207.   str[8]:=0
  208.   start:=Val(str+3,NIL)
  209.   end  :=Val(str+6,NIL)
  210.  
  211.   IF start<1 THEN RETURN -1
  212.   IF end<start THEN RETURN -1;
  213.  
  214.   IF (str[9]="Y") OR (str[9]="y") THEN flag:=1 ELSE flag:=0;
  215.  
  216.   FOR n:=start TO end
  217.     units[current].status   := UNIT_WAIT
  218.     units[current].prefix[0]:= str[0]
  219.     units[current].prefix[1]:= str[1]
  220.     units[current].id       := n
  221.     units[current].flag     := flag
  222.     current:=current+1
  223.     IF current>MAX_UNITS THEN RETURN -1
  224.   ENDFOR
  225.  
  226. ENDPROC 0
  227.  
  228.  
  229. /*********************************/
  230. /*** EasyGUI action procedures ***/
  231. /*********************************/
  232.  
  233.  
  234. /****************************/
  235. /*** Handle Ticker Action ***/
  236. /****************************/
  237.  
  238. PROC tiktoc()
  239.   current:=current+1
  240.   IF current>=totunits THEN current:=0
  241. ENDPROC
  242.  
  243. /******************************/
  244. /*** Toggle Log Writes Flag ***/
  245. /******************************/
  246.  
  247. PROC logtog()
  248.   IF log=TRUE THEN log:=FALSE ELSE log:=TRUE
  249. ENDPROC
  250.  
  251. /******************************/
  252. /*** Handle Next Button Hit ***/
  253. /******************************/
  254.  
  255. PROC nexthit()
  256.   DEF n,flg,count=0
  257.   DEF str1[10]:STRING
  258.  
  259.   REPEAT
  260.     count:=count+1
  261.  
  262.     current:=current+1
  263.     IF current>=totunits THEN current:=0
  264.  
  265.     IF count>totunits
  266.       /*** End of Round - Set Moved to Waiting ***/
  267.       butrequest('InitPool Request','End of round !!!!','  OK  ')
  268.       flg:=0
  269.       FOR n:=0 TO totunits-1
  270.         IF units[n].status=UNIT_MOVE
  271.           units[n].status:=UNIT_WAIT
  272.           flg:=1
  273.         ENDIF
  274.       ENDFOR
  275.       /*** Handle no remaining live elements ***/
  276.       IF flg=0
  277.         butrequest('InitPool Request','All dead - starting again !!!!','  OK  ')
  278.         FOR n:=0 TO totunits-1
  279.           units[n].status:=UNIT_WAIT
  280.         ENDFOR
  281.       ENDIF
  282.       last[0]:=0
  283.       count:=0
  284.     ENDIF
  285.   UNTIL units[current].status=UNIT_WAIT
  286.  
  287.   /** Set unit to moved & build ID string for it **/
  288.  
  289.   units[current].status:=UNIT_MOVE
  290.   n:=current
  291.   IF units[n].flag=0
  292.     StringF(str1,'\c\c\z\d[2]',units[n].prefix[0],units[n].prefix[1],units[n].id)
  293.   ELSE
  294.     StringF(str1,'\c\c \c',units[n].prefix[0],units[n].prefix[1],units[n].id+64)
  295.   ENDIF
  296.  
  297.   /** Build output string based on if prior move or not **/
  298.  
  299.   IF last[0]=0
  300.     StringF(dispstr,'Move:\s',str1)
  301.   ELSE
  302.     StringF(dispstr,'Last:\s Move:\s',last,str1)
  303.   ENDIF
  304.  
  305.   /** Store current ID string as prior string & update display **/
  306.  
  307.   StrCopy(last,str1)
  308.   lastn:=current
  309.   IF log THEN WriteF('\s\n',dispstr)
  310.   text_a.set(PLA_Text_Text, dispstr)
  311.  
  312. ENDPROC
  313.  
  314.  
  315. /***********************/
  316. /*** Show Units List ***/
  317. /***********************/
  318.  
  319. PROC unitslist() HANDLE
  320.   DEF active=0,ret=-1
  321.   DEF gh=NIL:PTR TO guihandle
  322.  
  323.  
  324.   blockwin(mgh)                                    /* Disable Window Input */
  325.   active:=1
  326.  
  327.   buildunitlist()                                  /* Build elements list */
  328.  
  329.   gh:=guiinitA('InitPool',                         /* Build EasyGUI Interface */
  330.               [ROWS,
  331.                 [LISTV,{unitsel},'Elements List',10,8,unitlist,FALSE,TRUE,NIL],
  332.                 [COLS,
  333.                   [SBUTTON,0,' DONE ']
  334.                 ]
  335.               ],NIL)
  336.  
  337.   WHILE ret<>0                                    /* Loop Until OK or Cancel Hit */
  338.     Wait(gh.sig)                                  /* Wait for IDCMP message      */
  339.     ret:=guimessage(gh)                           /* Pass message to EasyGUI     */
  340.   ENDWHILE
  341.  
  342. EXCEPT DO
  343.   IF gh THEN cleangui(gh)                         /* If open close down GUI */
  344.   IF unitlist THEN freeunitlist()                 /* Free units list */
  345.   IF active THEN unblockwin(mgh)                   /* Re-Enable Window Input */
  346. ENDPROC
  347.  
  348. /**********************************/
  349. /*** Handle List Item Selection ***/
  350. /**********************************/
  351.  
  352. PROC unitsel(qual,data,info,sel)
  353.   DEF str[10]:STRING,str1[80]:STRING
  354.   DEF flags[3]:ARRAY OF LONG
  355.   DEF ret,oldstate,newstate
  356.  
  357.   info:=info  /* Stop silly not used messages */
  358.   qual:=qual
  359.   data:=data
  360.  
  361.   flags[0]:='WAIT'
  362.   flags[1]:='MOVE'
  363.   flags[2]:='DEAD'
  364.  
  365.   oldstate:=units[sel].status
  366.   oldstate:=flags[oldstate]
  367.      
  368.   IF units[sel].flag=1
  369.     StringF(str,'\c\c \c',units[sel].prefix[0],units[sel].prefix[1],units[sel].id+64)
  370.   ELSE
  371.     StringF(str,'\c\c\z\d[2]',units[sel].prefix[0],units[sel].prefix[1],units[sel].id)
  372.   ENDIF
  373.   StringF(str1,'Change \s from \s to:',str,oldstate)
  374.   
  375.   ret:=butrequest('InitPool Request',str1,' Dead |Waiting| Moved |Cancel')
  376.   SELECT ret
  377.     CASE 0
  378.       newstate:=-1
  379.     CASE 1
  380.       newstate:=UNIT_DEAD
  381.     CASE 2
  382.       newstate:=UNIT_WAIT
  383.     CASE 3
  384.       newstate:=UNIT_MOVE
  385.   ENDSELECT
  386.  
  387.   IF newstate>=0
  388.     units[sel].status:=newstate
  389.     newstate:=flags[newstate]
  390.     IF log=TRUE THEN WriteF('\s=\s from \s\n',str,newstate,oldstate)
  391.     quitgui()
  392.   ENDIF
  393.  
  394. ENDPROC
  395.  
  396.  
  397. /*******************************************************/
  398. /*** Convert ObjDef Array to ExecList and Free Array ***/
  399. /*******************************************************/
  400.  
  401. PROC buildunitlist()
  402.   DEF n,m,state,ui:PTR TO unititem
  403.   DEF str[16]:STRING,flags[3]:ARRAY OF LONG
  404.  
  405.   flags[0]:='WAIT'
  406.   flags[1]:='MOVE'
  407.   flags[2]:='DEAD'
  408.  
  409.   IF unitlist<>NIL THEN freeunitlist()
  410.   unitlist:=newlist()
  411.  
  412.   FOR n:=0 TO totunits-1                       /* Loop throgh units array        */
  413.     ui:=New(SIZEOF unititem)                   /* Alloc RAM for new item node    */
  414.  
  415.     state:=units[n].status
  416.     state:=flags[state]
  417.     IF units[n].flag<>1                                /* Build Unit info string */
  418.       StringF(str,'\s     \c\c\z\d[2]',state,
  419.                                    units[n].prefix[0],
  420.                                    units[n].prefix[1],
  421.                                    units[n].id)
  422.     ELSE
  423.       StringF(str,'\s     \c\c \c',state,
  424.                                units[n].prefix[0],
  425.                                units[n].prefix[1],
  426.                                units[n].id+64)
  427.     ENDIF
  428.  
  429.     m:=0                                             /* Copy info string to node */
  430.     WHILE str[m]<>0
  431.       ui.name[m]:=str[m]
  432.       m:=m+1
  433.     ENDWHILE
  434.     ui.name[m]:=0
  435.  
  436.     ui.id:=n                                           /* Copy index ti id filed */
  437.     newnode(ui.node,ui.name,0,0)                  /* Initialise unit item's node */
  438.     Enqueue(unitlist,ui.node)                    /* Add node to current list end */
  439.   ENDFOR
  440.  
  441. ENDPROC
  442.  
  443. /*******************************************/
  444. /*** Free Contents of unititem Exec List ***/
  445. /*******************************************/
  446.  
  447. PROC freeunitlist()
  448.   DEF ui:PTR TO unititem,next:PTR TO unititem
  449.  
  450.   ui:=unitlist.head
  451.   WHILE ui.node.succ<>NIL
  452.     next:=ui.node.succ
  453.     Remove(ui.node)
  454.     Dispose(ui)
  455.     ui:=next
  456.   ENDWHILE
  457.  
  458.   unitlist:=NIL
  459. ENDPROC
  460.  
  461.  
  462.